# -*- coding: utf-8 -*-

"""
@Datetime: 2019/4/19
@Author: Zhang Yafei
"""
import logging
from pdfminer.pdfinterp import PDFResourceManager, process_pdf
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from io import StringIO


logging.propagate = False
logging.getLogger().setLevel(logging.ERROR)


def convert_pdf(path):
    rsrcmgr = PDFResourceManager()
    retstr = StringIO()
    laparams = LAParams()
    device = TextConverter(rsrcmgr, retstr, laparams=laparams)

    fp = open(path, 'rb')
    process_pdf(rsrcmgr, device, fp)
    fp.close()
    device.close()

    string = retstr.getvalue()
    retstr.close()
    # save_path = path.split('.pdf')[0] + '.txt'
    # save_txt(save_path, string)
    print(string)


def save_txt(path, content):
    with open(path, 'w', encoding='utf-8') as f:
        f.write(content)
    print(f'{path}保存成功')


if __name__ == '__main__':
    convert_pdf('1.pdf')